<?php
class GroupUtility {

	private static $_errorMessage = '';

	public static function getMessage() {
		return self::$_errorMessage;
	}

	private function __construct() {}

	/**
	 * get the group details
	* @return array
	*/
	public static function getGroup( $groupId ){
		$loginUsrId = Login::checkLoginStatus() == Login::USER_LOGGED_IN ? Login::getUserInfo( 'userId' ) : 0;
		if( $loginUsrId <= 0 ){
			throw new Exception( 'Your login session expired, Please login first!' );
		}
		$group = array();
		try{
			$objGroups = getMultiple( 'group', "groupId||$groupId" );
			foreach( $objGroups() as $objGroup ){
				$id = $objGroup->getGroupId();
				$members = array();
				$objMembers = getMultiple( 'groupMember', "groupId||$id", 'quantity||100' );
				foreach( $objMembers() as $objMember ){
					$userId = $objMember->getUserId();
					if( $userId > 0 ){
						$objUser = getMultiple( 'user', "userId||$userId", 'status||verified' );
						if( $objUser->getResultCount() > 0 ){
							$userId = $objUser->getUserId();
							$members[] = array(
								'name' => $objUser->getName(), 
								'userEmail' => $objUser->getUserEmail(),
								"status" => 'Active'
							);
						}
					}
				}
				$group = array(
					'id' => $id, 'name' => $objGroup->getTitle(), 
					'status' => ucwords( $objGroup->getStatus() ),
					'desc' => getShortText( $objGroup->getDescription(), 30 ),
					'link' => HOME_PATH  . "admin/groups/nv/id/$id/",
					'members' => $members
				);
			}
			return $group;
		}catch( Exception $ex ){
			throw $ex;
		}
	}

	/**
	 * get the group members details
	* @return array
	*/
	public static function getGroupMembers( $groupId ){
		$loginUsrId = Login::checkLoginStatus() == Login::USER_LOGGED_IN ? Login::getUserInfo( 'userId' ) : 0;
		if( $loginUsrId <= 0 ){
			throw new Exception( 'Your login session expired, Please login first!' );
		}
		$members = array();
		try{
			$objMembers = getMultiple( 'groupMember', "groupId||$groupId", 'quantity||100' );
			foreach( $objMembers() as $objMember ){
				$userId = $objMember->getUserId();
				if( $userId > 0 ){
					$objUser = getMultiple( 'user', "userId||$userId", 'status||verified' );
					if( $objUser->getResultCount() > 0 ){
						$userId = $objUser->getUserId();
						$members[] = array(
							'id' => $userId,
							'name' => $objUser->getName(), 
							'userEmail' => $objUser->getUserEmail()
						);
					}
				}
			}
			return $members;
		}catch( Exception $ex ){
			throw $ex;
		}
	}
	
	/**
	 * get the all groups
	* @return array
	*/
	public static function getGroups( $teamId, $quantity=30, $pageNumber=1 ){
		$loginUsrId = Login::checkLoginStatus() == Login::USER_LOGGED_IN ? Login::getUserInfo( 'userId' ) : 0;
		if( $loginUsrId <= 0 ){
			throw new Exception( 'Your login session expired, Please login first!' );
		}
		$groups = array();
		try{
			$objGroups = getMultiple( 'group', "teamId||$teamId", 
			"quantity||$quantity", "pageNumber||$pageNumber", 'paging||Y' );
			foreach( $objGroups() as $objGroup ){
				$id = $objGroup->getGroupId();
				$groups[] = array(
					'id' => $id, 'name' => $objGroup->getTitle(), 
					'status' => ucwords( $objGroup->getStatus() ),
					'desc' => getShortText( $objGroup->getDescription(), 30 ),
					'link' => HOME_PATH  . "admin/groups/nv/id/$id/tid/$teamId/",
					'members' => getMultiple( 'groupMember', "teamId||$teamId", "groupId||$id", 'count||Y' )->getTotalCount()
				);
			}
			return $groups;
		}catch( Exception $ex ){
			throw $ex;
		}
	}

	/**
	 * get the all active groups
	* @return array
	*/
	public static function getUserActiveGroupLists( $teamId ){
		$loginUsrId = Login::checkLoginStatus() == Login::USER_LOGGED_IN ? Login::getUserInfo( 'userId' ) : 0;
		if( $loginUsrId <= 0 ){
			throw new Exception( 'Your login session expired, Please login first!' );
		}
		$groups = array();
		try{
			$objGroups = getMultiple( 'group', "teamId||$teamId", 'status||active', "quantity||100" );
			foreach( $objGroups() as $objGroup ){
				$id = $objGroup->getGroupId();
				// check if user is part of the group or not
				/*if( getMultiple( 'groupMember', "status||active", "userId||$loginUsrId", "groupId||$id", 'count||Y' )->getTotalCount() == 0 ){
					continue;
				}*/
				// check if group's all members are in active state or not
				if( getMultiple( 'groupMember', "status||active", "groupId||$id", 'count||Y' )->getTotalCount() == 0 ){
					continue;
				}
				$groups[ $id ] = array(
					'id' => $id, 'text' => $objGroup->getTitle()
				);
			}
			return $groups;
		}catch( Exception $ex ){
			throw $ex;
		}
	}


	/**
	 * get active group active members lists
	* @return array
	*/
	public static function getActiveGroupMembers( $groupId ){
		$loginUsrId = Login::checkLoginStatus() == Login::USER_LOGGED_IN ? Login::getUserInfo( 'userId' ) : 0;
		if( $loginUsrId <= 0 ){
			throw new Exception( 'Your login session expired, Please login first!' );
		}
		$members = array();
		try{
			// check if user is part of the member or not
			$objMemberChk = getMultiple( 'groupMember', "groupId||$groupId", "userId||$loginUsrId", 'status||active', 'count||Y' );
			if( $objMemberChk->getTotalCount() == 0 ){
				return $members;
			}
			$objMembers = getMultiple( 'groupMember', "groupId||$groupId", 'status||active', 'quantity||100' );
			foreach( $objMembers() as $objMember ){
				$userId = $objMember->getUserId();
				if( $userId <= 0 ){
					continue;
				}
				$objUser = getMultiple( 'user', "userId||$userId", 'status||verified' );
				if( $objUser->getResultCount() <= 0 ){
					continue;
				}
				$members[ $userId ] = array(
					'id' => $userId, 'text' => $objUser->getName()
				);
			}
			return $members;
		}catch( Exception $ex ){
			throw $ex;
		}
	}

}
